﻿using App10.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using System.Text.RegularExpressions;

namespace App10.Models
{
    public class User : ModelBase<User>
    {
        public User()
            : base()
        {
            // defaults (for Revert())
            this.FirstName = string.Empty;
            this.LastName = string.Empty;
            this.AcceptTerms = false;

            // setup validator
            this.Validator = e =>
            {
                var property = this.Properties["FirstName"];
                if (string.IsNullOrWhiteSpace(this.FirstName))
                    property.Errors.Add("First Name is required");
                else if (this.FirstName.Length <= 3)
                    property.Errors.Add("First Name must be greater than 3 chars");
                if (this.FirstName.Equals("Jerry"))
                    property.Errors.Add("First Name cannot be 'Jerry'");

                property = this.Properties["LastName"];
                if (string.IsNullOrWhiteSpace(this.LastName))
                    property.Errors.Add("Last Name is required");
                else if (this.LastName.Length <= 3)
                    property.Errors.Add("Last Name must be greater than 3 chars");
                if (this.LastName.Equals("Nixon"))
                    property.Errors.Add("Last Name cannot be 'Nixon'");

                // email is not required
                property = this.Properties["Email"];
                property.IsEnabled = this.ReceiveCommunications;
                if (property.IsEnabled && !new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$").Match(this.Email).Success)
                    property.Errors.Add("Email must be valid format");

                property = this.Properties["AcceptTerms"];
                if (!this.AcceptTerms)
                    property.Errors.Add("You must accept the terms");
            };
        }

        public string FirstName { get { return GetProperty<string>(); } set { base.SetProperty(value); } }
        public string LastName { get { return GetProperty<string>(); } set { base.SetProperty(value); } }
        public bool ReceiveCommunications { get { return GetProperty<bool>(); } set { base.SetProperty(value); } }
        public string Email { get { return GetProperty<string>(); } set { base.SetProperty(value); } }
        public bool AcceptTerms { get { return GetProperty<bool>(); } set { base.SetProperty(value); } }
    }
}
